
--Custom Crosshair loader by mblunk
--no user friendly vars here, check out the Crosshair selection menu on the options tab of the spawnmenu

/*
|--------------------------------------------------------|
| Addon info				--|
|--------------------------------------------------------|
|							--|
| Author: 					--|
| * Elite Trooper					--|
|							--|
|							--|
| Features:					--|
| * Alpha control			--|
| * Easy enabling/disabling	--|
| * Size slider (6-128)		--|
| * Material gallery		--|
| * More than one hundred	--|
|	crosshairs to choose 	--|
|	from!					--|
|							--|
|--------------------------------------------------------|
*/
 
if SERVER then return end --it's a clientside script

----------------------------------------------------------------
//Setting up some database stuff
----------------------------------------------------------------
local Crosshair = {}
Crosshair.Materials = file.Find("../materials/Crosshair/*.vmt")
Crosshair.List 		= {}


----------------------------------------------------------------------------------------------
// Setting up all the client console variables
----------------------------------------------------------------------------------------------
Crosshair.Enabled 	= CreateClientConVar("Crosshair_enabled", 0, false, false)
Crosshair.Size 		= CreateClientConVar("Crosshair_size", 50, false, false)
Crosshair.Red 		= CreateClientConVar("Crosshair_c_r", 255, false, false )
Crosshair.Green 	= CreateClientConVar("Crosshair_c_g", 255, false, false )
Crosshair.Blue		= CreateClientConVar("Crosshair_c_b", 255, false, false )
Crosshair.Alpha 	= CreateClientConVar("Crosshair_c_a", 255, false, false )
Crosshair.Texture 	= CreateClientConVar("Crosshair_texture", "crosshair_010", false, false )


----------------------------------------------------------------------------------------------------------------------------------------------------------------
// The draw function, called on HUDPaint and draws if Crosshair.Enabled is true.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
function Crosshair.Draw()

	if Crosshair.Enabled:GetBool() then // Check if the custom crosshair is toggled on
	

	
		Crosshair.TexturePath = "Crosshair/" .. Crosshair.Texture:GetString() // Get the texture name from the panel.
		Crosshair.TextureID = surface.GetTextureID( Crosshair.TexturePath ) // Get the ID of the material (Some valve thingy-ma-jig.
	
	    surface.SetTexture( Crosshair.TextureID ) // Set the texture to draw with
	    surface.SetDrawColor( 255, 255, 255, Crosshair.Alpha:GetInt() ) // Colors doesn't work yet, so we'll just leave those at 255 and get the alpha from the panel.
	    surface.DrawTexturedRect( ScrW() / 2 - Crosshair.Size:GetInt() / 2, ScrH() / 2 - Crosshair.Size:GetInt() / 2, Crosshair.Size:GetInt(), Crosshair.Size:GetInt() ) // Position the crosshair
	

	
	end
	
end

hook.Add("HUDPaint", "Crosshair.Draw", Crosshair.Draw)
  

----------------------------------------------------------------------------------------
// Tell the panel what to act and look like.
----------------------------------------------------------------------------------------
function Crosshair.Panel( Panel )

	Panel:AddControl( "Header", { 
		Text = "Custom Crosshair Control Panel", 
		Description = "Adjust size, colors and material here." 
		}  
	)
	 
	Panel:AddControl("MaterialGallery", {
	    Label = "Crosshair choice",
	    Height = "64",
	    Width = "64",
	    Rows = "8",
	    Stretch = "1",
	 
	    Options = Crosshair.List,
	    
	    CVars = { [0] = "Crosshair_texture"	}
		}
	)
		
	Panel:AddControl("Checkbox", {
			Label = "Enable custom Crosshair", 
			Description = "Choose whether to show your regular Crosshair or your custom one.", 
			Command = "Crosshair_enabled" 
		}
	)

	Panel:AddControl("Color",{
			Label 	= "Color",
			Red 	= "Crosshair_c_r",
			Green 	= "Crosshair_c_g",
			Blue 	= "Crosshair_c_b",
			Alpha 	= "Crosshair_c_a",
			ShowAlpha	= "1",
			ShowHSV		= "0",
			ShowRGB		= "0",
			Multiplier	= "255",
		}
	)

	Panel:AddControl("Slider", {
			Label = "Crosshair size",
			Type = "Float",
			Min = "6",
			Max = "128",
			Command = "Crosshair_size"
		}
	)
		
end
	
	
-----------------------------------------------------------------------------------------------------------------------------------------------
// Add the "Crosshair" panel to the Player category in the Options tab.
-----------------------------------------------------------------------------------------------------------------------------------------------
function Crosshair.AddPanel()
	
	for k,v in pairs( Crosshair.Materials ) do
	    
		local Idx = string.sub(v,0,-5)
		
		Crosshair.List[Idx] = { Material = "Crosshair/"..Idx, Crosshair_texture = Idx }
	
	end
	
	spawnmenu.AddToolMenuOption( "Options", "Player", "Crosshair", "Crosshair", "", "", Crosshair.Panel, {} )

end
hook.Add( "PopulateToolMenu", "AddCrosshairPanel", Crosshair.AddPanel )